package com.chance.cc.crawler.development.bootstrap.autohome;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chance.cc.crawler.core.downloader.*;
import com.chance.cc.crawler.core.downloader.http.HttpClientDownloader;
import com.chance.cc.crawler.core.downloader.http.HttpClientGenerator;
import com.chance.cc.crawler.core.downloader.proxy.Proxy;
import com.chance.cc.crawler.core.downloader.proxy.SiteConfigProxyProvider;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.URLEncoder;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author lt
 * @version 1.0
 * @date 2021-05-13 10:37:43
 * @email okprog@sina.com
 */
public class DownloadTest  {
    private static Proxy proxy = new Proxy();
//    static {
//        //代理配置
//        //H5168QRFNIU3804D
//        //5F6B3610BB719FAA
//        proxy.setHost("http-dyn.abuyun.com");
//        proxy.setPort(9020);
//        proxy.setUsername("HL89Q19E86E2987D");
//        proxy.setPassword("71F33D94CE5F7BF2");
//
//    }
    static {
        //代理配置
        //H5168QRFNIU3804D
        //5F6B3610BB719FAA
        proxy.setHost("http-cla.abuyun.com");
        proxy.setPort(9030);
        proxy.setUsername("H3487178Q0I1HVPC");
        proxy.setPassword("ACE81171D81169CA");

    }
    public static final String abuyunPrefix = "http://proxy.abuyun.com/";
    public static final String switchIp = "switch-ip";
    public static final String currentIp = "current-ip";

    public static Logger logger = LoggerFactory.getLogger(DownloadTest.class);

    //熊猫
//    static {
//        //代理配置
//        //H5168QRFNIU3804D
//        //5F6B3610BB719FAA
//        proxy.setHost("dynamic.xiongmaodaili.com");
//        proxy.setPort(8089);
////        proxy.setUsername("HL89Q19E86E2987D");
////        proxy.setPassword("71F33D94CE5F7BF2");
//
//    }
    //小象
//    static {
//        //代理配置
//        //H5168QRFNIU3804D
//        //5F6B3610BB719FAA
//        proxy.setHost("http-dynamic-S03.xiaoxiangdaili.com");
//        proxy.setPort(10030);
//        proxy.setUsername("711075214733496320");
//        proxy.setPassword("m2SIllHs");
//
//    }
//    //代理云
//    static {
//        //代理配置
//        //H5168QRFNIU3804D
//        //5F6B3610BB719FAA
//        proxy.setHost("106.9.168.63");
//        proxy.setPort(57114);
//        proxy.setUsername("chancelt");
//        proxy.setPassword("chance@123");
//
//    }
    //芝麻
//    static {
//        //代理配置
//        //H5168QRFNIU3804D
//        //5F6B3610BB719FAA
//        proxy.setHost("115.209.247.41");
//        proxy.setPort(4245);
//        proxy.setUsername("chancelt");
//        proxy.setPassword("chance@123");
//
//    }

    public static void main(String[] args) {
        System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2,SSLv3");
        System.setProperty("jdk.http.auth.tunneling.disabledSchemes", "");

        HttpConfig httpConfig = HttpConfig.me("test");
        httpConfig.setProxy(proxy);
        httpConfig.setUseProxy(true);

        HttpClientDownloader downloader = new HttpClientDownloader();
        downloader.setClientGenerator(new HttpClientGenerator());
        downloader.setProxyProvider(new SiteConfigProxyProvider());
        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setUrl(abuyunPrefix + currentIp);

//        HttpPage download = downloader.download(httpRequest, httpConfig);
//        System.out.println(download.getRawText());

        for (int i = 1; i <= 1; i++) {
            new Thread(new TestDownload(httpConfig,downloader), "thread_" + i).start();
        }

//        String kwUrl = "http://192.168.1.215:9599//v1/meta/autohome/keys?site=series";
//        HttpRequest httpRequest = new HttpRequest();
//        httpRequest.setUrl(kwUrl);
//        HttpPage kwPage = downloader.download(httpRequest, httpConfig);
//        JSONObject jsonObject = JSONObject.parseObject(kwPage.getRawText());
//        JSONArray objects = jsonObject.getJSONArray("content");
////
//        String url = "https://club.autohome.com.cn/bbs/thread/58d37eb204069fcb/83867914-1.html";
//        httpConfig.setUseProxy(false);
//        System.out.println(objects.size());
//        List<Map<String,Long>> info = new ArrayList<>();
//        Set<String> ips = new HashSet<>();
//
//        long sumTime = 0L;
//        int f = 1;
//        for (Object object : objects) {
//            if (f > 500){
//                break;
//            }
//            for (int i = 0; i < 10; i++) {
//                try {
//                    Map<String,Long> urlTime = new HashMap<>();
//                    String keyword = ((JSONObject) object).getString("keyword");
//                    String indexUrl = String.format("https://www.autohome.com.cn/%s/", keyword);
////                    httpRequest.setUrl(indexUrl);
//                    httpRequest.setUrl(url);
////                    String orderNo = "";
////                    String secret = "";
////                    String times = String.valueOf(System.currentTimeMillis()).substring(0,10);
////                    String txt = "orderno="+orderNo+",secret="+secret+",timestamp=" + times;
////                    String sign = DigestUtils.md5Hex(txt).toUpperCase();
////                    httpRequest.addHeader("Proxy-Authorization","sign=" + sign);
//                    long startTime = System.currentTimeMillis();
//                    httpConfig.setDisableCookie(true);
//                    HttpPage download = downloader.download(httpRequest, httpConfig);
//                    if (download.isDownloadSuccess()){
//                        f++;
//                        long endTime = System.currentTimeMillis();
//                        long costTime = endTime - startTime;
//                        System.out.println(indexUrl + " 下载耗时 :" + costTime + " ms");
//                        urlTime.put(indexUrl,costTime);
//                        info.add(urlTime);
//                        sumTime += costTime;
//                        List<String> ipInfos = download.getHtml().xpath("/html/body/p[1]").all();
//                        StringBuilder ipInfo = new StringBuilder();
//                        for (String s : ipInfos) {
//                            ipInfo.append(s.trim());
//                        }
//                        Matcher matcher = Pattern.compile("\\d*\\.\\d*\\.\\d*\\.\\d*").matcher(ipInfo);
//                        while (matcher.find()){
//                            String ip = matcher.group(0);
//                            ips.add(ip);
//                        }
//                        System.out.println(ipInfo.toString());
//                    }
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//        int size = info.size();
//        System.out.println("总下载 " + size + " 次,耗时：" + sumTime + "ms" + ",平均下载时间: " + sumTime/size + "ms");
//        ips.forEach(System.out::println);

    }



}

class TestDownload implements Runnable{
    String url = "https://club.autohome.com.cn/bbs/thread/58d37eb204069fcb/83867914-1.html";
    public static final String abuyunPrefix = "http://proxy.abuyun.com/";
    public static final String switchIp = "switch-ip";
    public static final String currentIp = "current-ip";
    HttpConfig httpConfig;
    Downloader downloader;

    public TestDownload(HttpConfig httpConfig, Downloader downloader) {
        this.httpConfig = httpConfig;
        this.downloader = downloader;
    }

    @Override
    public void run() {
        String threadName = Thread.currentThread().getName();

        System.out.println(String.format("Thread [%s] start",threadName));
        int success = 0;
        int failed = 0;
        System.out.println(System.currentTimeMillis());
        httpConfig.setDisableCookie(true);
        for (int i = 0; i < 100; i++) {
            HttpRequest httpRequest = new HttpRequest();
            httpRequest.setUrl(url);
            Random random = new Random();

            httpRequest.addHeader("Host","club.autohome.com.cn");
//            httpRequest.addHeader("Cookie","pepopclub=CA5FB7C453DE16C6DE02F6FF38D1DAE2;");
            httpRequest.addHeader("User-Agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36");
            try {
                HttpRequest request = new HttpRequest();
                request.setUrl(abuyunPrefix + currentIp);
                HttpPage page = downloader.download(request, httpConfig);
                System.out.println("当前IP: " + page.getRawText());
                httpRequest.addHeader("cookie","sessionip=" + page.getRawText().split(",")[0] + ";");
                HttpPage httpPage = downloader.download(httpRequest, httpConfig);

                String rawText = httpPage.getRawText();
                int statusCode = httpPage.getStatusCode();
                if (!httpPage.isDownloadSuccess()){
                    System.out.println("download false");
                    continue;
                }
                if (statusCode != 200 && httpPage.isDownloadSuccess()){
                    System.out.println(String.format("status code is : [%s]",statusCode));
                    failed ++;
                    i --;
//                    doVerification(downloader,httpConfig,httpPage);
                    continue;
                }
                if (StringUtils.isNotBlank(rawText) && rawText.contains("尊敬的用户您好，您的访问出现异常，为确认本次访问为正常用户行为")){
                    System.out.println("页面出现ip验证请切换IP");
                    failed ++;
                    i --;
                    HttpRequest request1 = new HttpRequest();
                    request1.setUrl(abuyunPrefix + switchIp);
                    HttpPage download = downloader.download(request1, httpConfig);
                    System.out.println("IP已切换: " + download.getRawText());
//                    doVerification(downloader,httpConfig,httpPage);
                    Thread.sleep(5000);
                    continue;
                }
                success++;
                System.out.println(String.format("Thread [%s] download page success : page [%s] ", threadName,url));
                System.out.println(System.currentTimeMillis());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println(String.format("Thread [%s] download finished : success page [%s] ,failed page [%s]", threadName,success,failed));

    }

    private void doVerification(Downloader downloader, HttpConfig httpConfig,HttpPage httpPage) throws InterruptedException {
        String ip = null;
        try {
            HttpRequest ipReq = new HttpRequest();
            ipReq.setUrl(abuyunPrefix + currentIp);
            HttpPage ipPage = downloader.download(ipReq, httpConfig);
            ip = ipPage.getRawText().split(",")[0];
        } catch (Exception e) {
            ip = "180.164.48.4";
        }
        String verificationUrl = "http://safety.autohome.com.cn/userverify/verify_v4";

        HttpRequest httpRequest = new HttpRequest();
        httpRequest.setUrl(verificationUrl);
        httpRequest.setMethod("POST");
        String param = "backurl=#url%253Fqanotice%253D10&locnum=109%2C707&captchaVerification=1---%7B%22x%22%3A176.7%2C%22y%22%3A5%7D&challenge=1&notificationurl=http%253A%252F%252Frequestanalyzeapi.autohome.com.cn%252Fapi%252FRequestLimited%252FHumanValidatedNotification%253Fbid%253Dclub%2526amp%253Buri%253Dclub.autohome.com.cn%252Fbbs%252Fthread.*%2526amp%253BclientIp%253D#IP&r=#RANDOM";
        param = param.replace("#url", URLEncoder.encode(URLEncoder.encode(httpPage.getRequest().getUrl().split(":")[1]))).replace("#IP",ip).replace("#RANDOM",String.valueOf(Math.random()));
        HttpRequestBody httpRequestBody = HttpRequestBody.custom(param.getBytes(), "application/x-www-form-urlencoded; charset=UTF-8", "utf-8");
        httpRequest.setRequestBody(httpRequestBody);
        HttpPage page = downloader.download(httpRequest, httpConfig);
        System.out.println(page.getRawText());
        Thread.sleep(2000);

        httpRequest.setUrl(abuyunPrefix + switchIp);
        HttpPage download = downloader.download(httpRequest, httpConfig);
        System.out.println("IP已切换: " + download.getRawText());

    }
}
